//index.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    city: "",
    today: {},
    forecast: {},
    citys: ['北京', '上海', '深圳', '成都', '重庆', '广州', '东莞', '长沙', '株洲', '武汉', '绵阳', '长沙', '益阳', '南充']
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.LoadLoaction()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  //获取经纬度，定位城市
  LoadLoaction: function() {
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function(res) {
        //console.log(res)
        var latitude = res.latitude //经度
        var longitude = res.longitude //纬度
        that.loadCity(latitude, longitude)
      },
    })
  },
  //根据经纬度获取城市
  loadCity: function(latitude, longitude) {
    var that = this;
    wx.request({
      url: 'http://api.map.baidu.com/geocoder/v2/?location=' + latitude + '+,' + longitude + '&output=json&ak=DNxLRjceCBP8qfInMgiBzjLwcaOIVMvx',
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        var city = (res.data.result.addressComponent.city);
        city = city.replace("市", "")
        that.setData({
          city: city
        })
        that.loadWeather(city)
      }
    })
  },
  loadWeather: function(city) {
    var that = this
    wx.request({
      url: 'http://wthrcdn.etouch.cn/weather_mini?city=' + city,
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        //console.log(res)

        var today = res.data.data; //获取今日天气
        //正确的城市返回 status==1000
        if (res.data.status != 1000) {
          wx.showModal({
            title: '提示',
            content: '请正确输入城市名和县区名称！',
            success(res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        } else {
          //获取当前和未来天气
          var forecast = res.data.data.forecast;
          var todayInfo = forecast.shift()
          today.todayInfo = todayInfo

          that.setData({
            city: city,
            today: today,
            forecast: forecast
          })
          //console.log(today)
        }

      },

    })
  },
  //通过输入城市名称动态查询天气信息
  citySearchEvent: function(e) {
    //console.log(e.detail.value.textCity)
    var that = this
    var city = e.detail.value.textCity
    if (city.length == 0) {
      wx.showModal({
        title: '提示',
        content: '请输入城市名称',
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {
      that.loadWeather(city)
    }
  },
  //点击
  hotCityEvent: function(e) {
    var that = this
    var city = e.target.dataset.city
    that.loadWeather(city)
  }
})